using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using System.IO.Compression;
using System.Text;
using System.Reflection;
using System.Web.Routing;

namespace NeosSDI.Core.Manager
{
    /// <summary>
    /// Debug Manager Singleton
    /// </summary>
    public class DebugModeManager
    {
        /// <summary>
        /// Debug Manager instance
        /// </summary>
        private static DebugMode instance;

        /// <summary>
        /// Debug Manager Instance
        /// </summary>
        public static DebugMode Instance
        {
            get
            {
                if (instance == null)
                    instance = new DebugMode();
                return instance;
            }
        }
    }

    /// <summary>
    /// Debug Manager
    /// </summary>
    public class DebugMode
    {
        /// <summary>
        /// Defined if Application is started in debug mode
        /// </summary>
        private bool? isDebug;

        /// <summary>
        /// Defined if Application is started in debug mode
        /// </summary>
        /// <param name="assembly">Assembly to check</param>
        /// <returns>true if debug, false otherwise</returns>
        public bool IsDebug(Assembly assembly)
        {
            if (isDebug != null)
                return isDebug.Value;
            object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);
            if (attributes == null || attributes.Length == 0)
            {
                isDebug = true;
                return true;
            }
            var d = (DebuggableAttribute)attributes[0];
            if (d.IsJITTrackingEnabled)
            {
                isDebug = true;
                return true;
            }
            isDebug = false;
            return false;
        }
        /// <summary>
        /// Defined if Application is started in debug mode
        /// </summary>
        /// <returns>true if debug, false otherwise</returns>
        public bool IsDebug()
        {
            if (isDebug != null)
                return isDebug.Value;
            Assembly assembly = System.Web.Compilation.BuildManager.GetGlobalAsaxType().BaseType.Assembly;
            object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);
            if (attributes == null || attributes.Length == 0)
            {
                isDebug = true;
                return true;
            }
            var d = (DebuggableAttribute)attributes[0];
            if (d.IsJITTrackingEnabled)
            {
                isDebug = true;
                return true;
            }
            isDebug = false;
            return false;
        }

    }
}
